<template>
  <div class="app-container">
    <template v-for="item in auditConfigList">
      <configItem v-if="item.title" :value="item" @open-flow="handelOpenFlow" />
      <configItem v-else :value="item" @open-flow="handelOpenFlow">
        <template #title="{ record }">
          <template v-if="(record.customType = 'start')">
            开工申请必须提前<a-input
              v-model="time"
              :min="0"
              :max-length="4"
              @blur="changeStartTime"
              @keyup.enter="changeStartTime"
              @input="setTime"
            ></a-input
            >天
          </template>
        </template>
      </configItem>
    </template>
  </div>
</template>

<script setup>
import { Message } from "@arco-design/web-vue";
import configItem from "./components/configItem.vue";
import { editDays, getDays } from "@/apis/engineeringSettings/auditConfig";
import { useUserStore } from "@/stores";

const emit = defineEmits(["openFlow"]);
const userStore = useUserStore();

const time = ref(0);
const auditConfigList = [
  {
    customType: "start",
    desc: "设置0天为不限制，否则提前开工需要审批",
    params: { formId: `form-buildProcStart-${userStore.userInfo?.tenantId}` }
  },
  {
    title: "验收节点审批配置",
    desc: "设置验收审批人，提交验收后由该审批人进行内部审批",
    params: { formId: `form-acceptance-${userStore.userInfo?.tenantId}` }
  },
  // {
  //   title: "材料采买审批配置",
  //   desc: "设置材料申请审批人，审批通过后才发起采购"
  // },
  // {
  //   title: "工程请款审批设置",
  //   desc: "发包制项目经理请款审批"
  // },
  {
    title: "停工审批配置",
    desc: "工地停工审批",
    params: { formId: `form-shutdown-${userStore.userInfo?.tenantId}` }
  }
];
const changeStartTime = async () => {
  // if (!time.value) {
  //   Message.error("请输入天数");
  //   getDay();
  //   return;
  // }
  try {
    await editDays(time.value);
    Message.success("修改成功");
  } catch (error) {
    console.log(error);
  }
};
const getDay = async () => {
  try {
    const { data } = await getDays();
    time.value = data;
  } catch (error) {
    console.log(error);
  }
};
const setTime = value => {
  // 移除非数字字符
  let sanitizedValue = value.replace(/[^0-9]/g, "");
  // 确保值不超过四位
  if (sanitizedValue.length > 4) {
    sanitizedValue = sanitizedValue.slice(0, 4);
  }
  // 将值转换为数字
  time.value = sanitizedValue ? parseInt(sanitizedValue, 10) : "";
};
const handelOpenFlow = data => {
  emit("openFlow", data);
};

onMounted(() => {
  getDay();
});
</script>

<style lang="scss" scoped>
::v-deep(.arco-input) {
  width: 100px;
}
</style>
